/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Client_GUI.SavingType;

import Client_GUI.Customer.*;
import Client_GUI.Unity;
import Interface.IRemoteCustomer;
import Interface.IRemoteSavingType;
import Server.DTO.Customer_DTO;
import Server.DTO.SavingType_DTO;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 10520_000
 */
public class JP_UpdateSavingType extends org.jdesktop.swingx.JXPanel {

    Registry registry;
    IRemoteSavingType rsavingtype;
    Customer_DTO info;

    /**
     * Creates new form JP_SavingTypeAddCustomer
     */
    public JP_UpdateSavingType(SavingType_DTO info) throws RemoteException, NotBoundException {
        initComponents();
        Vector vt = new Vector();
        vt.add(1);
        vt.add(3);
        vt.add(6);
        vt.add(9);
        vt.add(12);
        vt.add(24);
        vt.add(36);
        txt_MaLoaiTietKiem.setText(info.getTypeID());
        if(info.getTerm()!=0)
        {
            cbo_TenLoaiTietKiem.setSelectedIndex(0);
            cbo_KyHan.setSelectedItem(cbo_KyHan.getItemAt(vt.indexOf(info.getTerm())));
        }
        else
        {
            cbo_TenLoaiTietKiem.setSelectedIndex(1);
            cbo_KyHan.setSelectedItem(cbo_KyHan.getItemAt(7));
        }
            
        //gan ky han
        
        
        txt_LaiXuat.setText(info.getInterestRate() + "");
        checkbox_Gui.setSelected(info.getAllowSave());
        checkbox_rut.setSelected(info.getAllowWithdraw());
        registry = LocateRegistry.getRegistry("127.0.0.1");
        rsavingtype = (IRemoteSavingType) registry.lookup("SavingType");
    }

    public void ClearAll() {
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btn_CapNhat = new javax.swing.JButton();
        btn_NhapLai = new javax.swing.JButton();
        btn_thoat = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txt_LaiXuat = new javax.swing.JTextField();
        cbo_TenLoaiTietKiem = new javax.swing.JComboBox();
        cbo_KyHan = new javax.swing.JComboBox();
        checkbox_Gui = new javax.swing.JCheckBox();
        checkbox_rut = new javax.swing.JCheckBox();
        jLabel8 = new javax.swing.JLabel();
        txt_MaLoaiTietKiem = new javax.swing.JTextField();

        btn_CapNhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/32/Small_AddCustomer.png"))); // NOI18N
        btn_CapNhat.setText("Cập Nhật");
        btn_CapNhat.setToolTipText("");
        btn_CapNhat.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn_CapNhat.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btn_CapNhat.setIconTextGap(40);
        btn_CapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CapNhatActionPerformed(evt);
            }
        });

        btn_NhapLai.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/32/Small_Update.png"))); // NOI18N
        btn_NhapLai.setText("Nhập lại");
        btn_NhapLai.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn_NhapLai.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btn_NhapLai.setIconTextGap(40);
        btn_NhapLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_NhapLaiActionPerformed(evt);
            }
        });

        btn_thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/32/Small_Close.png"))); // NOI18N
        btn_thoat.setText("Thoát");
        btn_thoat.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn_thoat.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btn_thoat.setIconTextGap(40);
        btn_thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_thoatActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khách hàng"));

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel2.setText("Tên Loại Tiết Kiệm");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel3.setText("Kỳ Hạn");

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel4.setText("Lãi Xuất");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel5.setText("Cho Phép Gửi");

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel6.setText("Cho Phép Rút");

        txt_LaiXuat.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N

        cbo_TenLoaiTietKiem.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CO KY HAN", "KHONG KY HAN", " " }));
        cbo_TenLoaiTietKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbo_TenLoaiTietKiemActionPerformed(evt);
            }
        });

        cbo_KyHan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1 Tháng", "3 Tháng", "6 Tháng", "9 Tháng", "12 Tháng", "24 Tháng ", "36 Tháng", "Không Kỳ Hạn", " " }));
        cbo_KyHan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbo_KyHanActionPerformed(evt);
            }
        });

        checkbox_Gui.setSelected(true);

        checkbox_rut.setSelected(true);

        jLabel8.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        jLabel8.setText("Mã Loại Tiết Kiệm");

        txt_MaLoaiTietKiem.setFont(new java.awt.Font("Times New Roman", 0, 15)); // NOI18N
        txt_MaLoaiTietKiem.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6))
                        .addGap(66, 66, 66))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel8)
                        .addGap(69, 69, 69)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_MaLoaiTietKiem, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txt_LaiXuat, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                            .addComponent(cbo_TenLoaiTietKiem, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(cbo_KyHan, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(checkbox_Gui)
                        .addComponent(checkbox_rut)))
                .addGap(41, 41, 41))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 19, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txt_MaLoaiTietKiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2)
                    .addComponent(cbo_TenLoaiTietKiem, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cbo_KyHan, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txt_LaiXuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(checkbox_Gui))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(checkbox_rut)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btn_CapNhat, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_NhapLai, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btn_thoat, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(btn_CapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btn_NhapLai, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btn_thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(36, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btn_CapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CapNhatActionPerformed
        // TODO add your handling code here:
        SavingType_DTO info = new SavingType_DTO();
        info.setTypeID(txt_MaLoaiTietKiem.getText());
        info.setName(cbo_TenLoaiTietKiem.getSelectedItem().toString());
        if (cbo_KyHan.isEnabled()) {
            String[] str_split = cbo_KyHan.getSelectedItem().toString().split(" ");
            info.setTerm(Integer.parseInt(str_split[0]));
        }
        else
        {
            info.setTerm(0);
        }

        info.setInterestRate(Float.parseFloat(txt_LaiXuat.getText()));
        info.setAllowSave(checkbox_Gui.isSelected());
        info.setAllowWithdraw(checkbox_rut.isSelected());

        try {
            rsavingtype.Update(info);
            JOptionPane.showMessageDialog(null, "Update thanh cong and reload table");
            Unity.AddPanel(this, new JP_SavingType());
        } catch (RemoteException ex) {
            Logger.getLogger(JP_AddCustomer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(JP_UpdateSavingType.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btn_CapNhatActionPerformed

    private void btn_NhapLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_NhapLaiActionPerformed
        // TODO add your handling code here:
        ClearAll();
    }//GEN-LAST:event_btn_NhapLaiActionPerformed

    private void btn_thoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_thoatActionPerformed
        try {
            // TODO add your handling code here:
            Unity.AddPanel(this, new JP_SavingType());
        } catch (RemoteException ex) {
            Logger.getLogger(JP_AddCustomer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(JP_AddCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(true);
    }//GEN-LAST:event_btn_thoatActionPerformed

    private void cbo_TenLoaiTietKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbo_TenLoaiTietKiemActionPerformed
        // TODO add your handling code here:
        if (cbo_TenLoaiTietKiem.getSelectedItem().toString().compareToIgnoreCase("KHONG KY HAN") == 0) {
            //set thang cho loi tiet kiem
            cbo_KyHan.setSelectedItem(cbo_KyHan.getItemAt(7));
            cbo_KyHan.setEnabled(false);

            //CHI CHO PHEP RUT, KHONG CHO PHEP GUI
            checkbox_rut.setSelected(true);
            checkbox_Gui.setSelected(false);
        } else {
            //CHO PHEP GUI, CHO PHEP RUT
            cbo_KyHan.setEnabled(true);
            cbo_KyHan.setSelectedItem(cbo_KyHan.getItemAt(0));
            checkbox_rut.setSelected(true);
            checkbox_Gui.setSelected(true);
        }
    }//GEN-LAST:event_cbo_TenLoaiTietKiemActionPerformed

    private void cbo_KyHanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbo_KyHanActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbo_KyHanActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_CapNhat;
    private javax.swing.JButton btn_NhapLai;
    private javax.swing.JButton btn_thoat;
    private javax.swing.JComboBox cbo_KyHan;
    private javax.swing.JComboBox cbo_TenLoaiTietKiem;
    private javax.swing.JCheckBox checkbox_Gui;
    private javax.swing.JCheckBox checkbox_rut;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txt_LaiXuat;
    private javax.swing.JTextField txt_MaLoaiTietKiem;
    // End of variables declaration//GEN-END:variables
}
